<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<script src="script/jquery/jquery-1.8.3.js"></script>
<script src="script/jquery/jquery-ui-1.9.2.custom.js"></script>
<link rel="stylesheet" href="css/jquery-ui-1.9.2.custom.css" />
<link rel="stylesheet" href="css/jquery-ui.css" />
</head>

<body>
	<script>
$(document).ready(
		function() {
			$( "#txtSearch1").focus();
			$( "#txtSearch1").autocomplete({source:'tools/ajax/searchSuggest.php',minLength:2});
			$( "#txtSearch1").autocomplete({
				select: function( event, ui ) {
					var data=ui.item.data;
					$("#prenom1").val(data['prenom']);
					$("#y1").val(data['y1']);
					$("#club1").val(data['club']);
					if(data['sexe']=='M'){
						$("#man1").attr('checked', true);
					}else{
						$("#woman1").attr('checked', true);
					}
					
				},
				close: function( event, ui ) {
					<?php if (Config::team){?>$( "#txtSearch2").focus()<?php }else{?>
					$("#dossard").focus();
					<?php }?>;
				}
				
			});
			<?php if (Config::team){?>
			$( "#txtSearch2").autocomplete({source:'tools/ajax/searchSuggest.php',minLength:2});
			$( "#txtSearch2").autocomplete({
				select: function( event, ui ) {
					var data=ui.item.data;
					$("#prenom2").val(data['prenom']);
					$("#y2").val(data['y1']);
					$("#club2").val(data['club']);
					if(data['sexe']=='M'){
						$("#man2").attr('checked', true);
					}else{
						$("#woman2").attr('checked', true);
					}
					
				},
				close: function( event, ui ) {
					$("#dossard").focus();
				}
				
			});
			<?php }?>
		}
);
</script>
	<?php

	require_once 'entity/Runner.php';
	require_once 'entity/Team.php';
	require_once 'tools/service/tools.php';
	require_once 'tools/http/HTTP_REQUEST.php';
	require_once 'tools/constant/Config.php';

	$dbUtils=new DbUtils();
	$lastOldDossard="0";
	$lastNewDossard="0";
	$erroMsg;
	if(HTTP_RESQUEST::exist('dossard_old')){
		$dbUtils->connect();
		if(!$dbUtils->isDossardInCurrentCourse(HTTP_RESQUEST::post('dossard_old'))){
			if($dbUtils->isDossardValidForReSubscribe(HTTP_RESQUEST::post('dossard_old'))){
				$dbUtils->subscribeOld(HTTP_RESQUEST::post('dossard_old'));
				$lastOldDossard=HTTP_RESQUEST::post('dossard_old');
			}else{
				$erroMsg="LE DOSSARD ".HTTP_RESQUEST::post('dossard_old')." N'A JAMAIS ETE PREALABLEMENT INSCRIT";
			}


		}else{
			$erroMsg="LE DOSSARD ".HTTP_RESQUEST::post('dossard_old')." EST DEJA INSCRIT SUR CETE COURSE";
		}

	}else{

		$runner1=new Runner(HTTP_RESQUEST::post('nom1'), HTTP_RESQUEST::post('prenom1'),HTTP_RESQUEST::post('y1')."-".HTTP_RESQUEST::post('m1')."-".HTTP_RESQUEST::post('d1'), HTTP_RESQUEST::post('sexe1'), HTTP_RESQUEST::post('club1'));
		$runner2=new Runner(HTTP_RESQUEST::post('nom2'), HTTP_RESQUEST::post('prenom2'),HTTP_RESQUEST::post('y2')."-".HTTP_RESQUEST::post('m2')."-".HTTP_RESQUEST::post('d2'), HTTP_RESQUEST::post('sexe2'), HTTP_RESQUEST::post('club2'));
		
		
		$canSave=false;
		if(Config::team){
			$canSave=$runner1->isValid() && $runner2->isValid() ;			
		}else{
			$canSave=$runner1->isValid();
		}

		
		
		if( $canSave){
		$dbUtils->connect();
		if(HTTP_RESQUEST::exist('dossard')){
			if( $dbUtils->isDossardValid(HTTP_RESQUEST::post('dossard'))){
				$team=new Team($runner1, $runner2, HTTP_RESQUEST::post('dossard'));
				$team->persist($dbUtils);
				
				$dbUtils->subscribeOld(HTTP_RESQUEST::post('dossard'),HTTP_RESQUEST::post('distance'));
				$dbUtils->close();
				$lastNewDossard=HTTP_RESQUEST::post('dossard');
			}else{
				$erroMsg="LE DOSSARD ".HTTP_RESQUEST::post('dossard')." EST DEJA INSCRIT SUR CETE COURSE";
			}
		}else {
			$erroMsg="VEUILLEZ ENTRER UN NUMERO DE DOSSARD VALIDE SVP";
		}


	}
	}

	?>
	<fieldset>
		<LEGEND align="left" class="main">
			Inscriptions <?php if(!Config::team){?><strong> - Dernier dossard nouvellement inscrit:  <span <?php if($lastNewDossard!='0'){?>style="color: red;" <?php }?>><?php echo $lastNewDossard;?></span></strong><?php }?> 
		</LEGEND>
		<?php if(isset($erroMsg)){?>
		<fieldset>
			<LEGEND align="left" style="color: red;" class="main">ERREUR !!! </LEGEND>
			<br> <strong><span style="color: red;"><?php echo $erroMsg; ?>
			
			</strong></span>
		</fieldset>
		<?php }?>
		<table>
			<tr>
				<td style="vertical-align: top;">
					<form action="./index.php?page=inscriptions.php" method="post">
						<table>
							<tr>
								<td>
									<table>
										<?php if(Config::team) {?>
										<tr>
										
										
											<td>

												<fieldset>
													<LEGEND align="left">Participant 1</LEGEND>
													<table>
														<tr>
															<td class="style1"><strong>Nom:&nbsp; 1</strong></td>
															<td><input type="text" id="txtSearch1" name="nom1"
																alt="Search Criteria" />
															</td>
														</tr>
														<tr>
															<td class="style1"><strong>Pr&eacute;nom:&nbsp; 1</strong>
															</td>
															<td><strong> <input name="prenom1" type="text"
																	id="prenom1">
															</strong></td>
														</tr>
														<tr>
															<td class="style1"><strong>Date de naissance:&nbsp; 2</strong>
															</td>
															<td><strong> <input name="d1" type="hidden" id="d1"
																	size="2" value="00" readonly="true" /> <input name="m1"
																	type="hidden" id="m1" size="2" value="00"
																	readonly="true" /> <input name="y1" type="text" id="y1"
																	size="10" maxlength="4" />
															</strong>
															</td>
														</tr>
														<tr>
															<td class="style1"><strong>Sexe:&nbsp;</strong></td>
															<td><strong> <label> 
																<input type="radio" name="sexe1" value="M" checked="true" id="man1"> M
																</label> <br> <label> 
																<input type="radio" name="sexe1" value="F" id="woman1"> F
																</label> <br>
															</strong></td>
														</tr>
														<tr>
															<td class="style1"><strong>Club:&nbsp;</strong></td>
															<td><strong> <input name="club1" type="text" id="club">
															</strong>
															</td>
														</tr>

													</table>
												</fieldset>
											</td>
											<td>
												<fieldset>
													<LEGEND align="left">Participant 2</LEGEND>
													<table>
														<tr>
															<td class="style1"><strong>Nom:&nbsp; 2</strong></td>
															<td><input type="text" id="txtSearch2" name="nom2"
																alt="Search Criteria"
																/>
															</td>
														</tr>
														<tr>
															<td class="style1"><strong>Pr&eacute;nom:&nbsp; 2</strong>
															</td>
															<td><strong> <input name="prenom2" type="text"
																	id="prenom2">
															</strong></td>
														</tr>
														<tr>
															<td class="style1"><strong>Date de naissance:&nbsp; 2</strong>
															</td>
															<td><strong> <input name="d2" type="hidden" id="d2"
																	size="2" value="00" readonly="true"> <input name="m2"
																	type="hidden" id="m2" size="2" value="00"
																	readonly="true"> <input name="y2" type="text" id="y2"
																	size="10" maxlength="4">
															</strong>
															</td>
														</tr>
														<tr>
															<td class="style1"><strong>Sexe:&nbsp;</strong></td>
															<td><strong> <label> <input type="radio" name="sexe2"
																		value="M" checked="true" id="man2"> M
																</label> <br> <label> <input type="radio" name="sexe2"
																		value="F" id="woman2"> F
																</label> <br>
															</strong></td>
														</tr>

														<tr>
															<td class="style1"><strong>Club:&nbsp;</strong></td>
															<td><strong> <input name="club2" type="text" id="club">
															</strong>
															</td>
														</tr>

													</table>
												</fieldset>
											</td>

										</tr>
									</table>
								</td>
							</tr>
							<tr>

								<td>
									<fieldset>
										<LEGEND align="left">Equipe</LEGEND>
										<table>
											<tr>
												<td class="style1"><strong>Dossard:&nbsp;</strong></td>
												<td><strong> <input name="dossard" type="text" id="dossard"
														autocomplete="off">
												</strong> <br> <strong>Dernier dossard nouvellement inscrit:
														<span <?php if($lastNewDossard!='0'){?>
														style="color: red;" <?php }?>><?php echo $lastNewDossard;?>
													</span>
												</strong>
												</td>
											</tr>

										</table>
										<strong><input type="submit" name="Submit" value="Submit"> </strong>
									</fieldset>
								</td>
							</tr>
							<?php } else { ?>
							<tr>
										
										
											<td>

												<fieldset class="participant">
													<LEGEND align="left">Participant</LEGEND>
													<table>
														<tr>
															<td class="style1"><strong>Nom:&nbsp;</strong></td>
															<td><input type="text" id="txtSearch1" name="nom1"
																alt="Search Criteria" />
															</td>
														</tr>
														<tr>
															<td class="style1"><strong>Pr&eacute;nom:&nbsp;</strong>
															</td>
															<td><strong> <input name="prenom1" type="text" id="prenom1">
															</strong></td>
														</tr>
														<tr>
															<td class="style1"><strong>Date de naissance:&nbsp;</strong>
															</td>
															<td><strong> <input name="d1" type="hidden" id="d1"
																	size="2" value="00" readonly="true" /> <input name="m1"
																	type="hidden" id="m1" size="2" value="00"
																	readonly="true" /> <input name="y1" type="text" id="y1"
																	size="10" maxlength="4" />
															</strong>
															</td>
														</tr>
														<tr>
															<td class="style1"><strong>Dossard:&nbsp;</strong></td>
															<td><strong> <input name="dossard" type="text" id="dossard"	autocomplete="off"></strong></td>	
														</tr>
														<tr>
															<td class="style1"><strong>Sexe:&nbsp;</strong></td>
															<td>
																<strong>
																	<label><input type="radio" name="sexe1" value="M" checked="true" id="man1"> M</label> 
																	<label><input type="radio" name="sexe1" value="F" id="woman1"> F</label> <br>
																</strong>
															</td>
														</tr>
														<tr>
														<td class="style1"><strong>Distance:&nbsp;</strong></td>
															<td>
																<strong> 
																	<label><input type="radio" name="distance" value="50" checked="true" > 50</label>
																	<label><input type="radio" name="distance" value="25" > 25</label> <br>
																</strong>
															</td>
														</tr>
														<tr>
															<td class="style1"><strong>Club:&nbsp;</strong></td>
															<td><strong> <input name="club1" type="text" id="club">
															</strong>
															</td>
														</tr>
														<tr><td colspan="2" class="style1"><strong><input type="submit" name="Submit" value="Submit"> </strong></tr></tr>
													</table>
												</fieldset>
											</td>
							<?php }?>
						</table>
					</form>
				</td>
				<?php if(Config::team){?>
				<td style="vertical-align: top;">
					<form action="./index.php?page=inscriptions.php" method="post">
						<table>
							<tr>
								<td>
									<fieldset>
										<LEGEND align="left">Equipe déja inscrite</LEGEND>
										<table>
											<tr>
												<td class="style1"><strong>Dossard:&nbsp;</strong></td>
												<td><strong> <input name="dossard_old" type="text"
														id="dossard_old" autocomplete="off">
												</strong> <br> <strong>Dernier dossard réinscrit: <span
												<?php if($lastOldDossard!='0'){?> style="color: red;"
												<?php }?>><?php echo $lastOldDossard;?> </span>
												</strong>
												</td>

											</tr>

										</table>
										<strong><input type="submit" name="Submit" value="Submit"> </strong>
									</fieldset>
								</td>
							</tr>
						</table>
					</form>
				</td>
				<?php }?>
			</tr>
		</table>

	</fieldset>


	<form id="frmSearch" action=""></form>
	<script>setFocusOn('txtSearch1');</script>
</body>
</html>
